using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class VieworUtils
{
		public static void setViewActive (Transform transform, bool status)
		{
				transform.gameObject.SetActive (status);
		}

		public static bool setAutoLayout (Transform transform, float scaleX, float y)
		{
				List<Transform> views = new List<Transform> ();
				List<UIViewAuto> viewAutos = new List<UIViewAuto> ();
				List<UIPanel> panelAutos = new List<UIPanel> ();
				foreach (Transform trans in transform) {
						UIPanel panel = trans.gameObject.GetComponent<UIPanel> ();
						if (panel == null || !panel.enabled || panel.clipping == UIDrawCall.Clipping.None) {
								UIViewAuto view = trans.gameObject.GetComponent<UIViewAuto> ();
								if (view != null && view.enabled) {
										viewAutos.Add ((UIViewAuto)view);

								} else {
										if (trans.localPosition.y != 0) {
												views.Add (trans);
										}
								}

						} else {
								float height = panel.clipRange.w;
								if (height > 0) {
										panelAutos.Add (panel);
								}
						}
				}

				if (viewAutos.Count == 0 && panelAutos.Count == 0) {
						if (transform.childCount == 1) {
								Transform view = transform.GetChild (0);
								if (setAutoLayout (view, scaleX, y)) {
										UIViewAuto viewAuto = GameObjectUtils.getGameObjectComponent<UIViewAuto> (view.gameObject);
										viewAuto.contentSize.y = y;
										// viewAuto.setHeight(y);
										viewAuto.setAutoScaleX (scaleX);
										return true;
								}
						}

						return false;
				}

				foreach (Transform view in views) {
						autoPos (view, scaleX, y);
				}

				float heightOffset = y / scaleX - y;
				foreach (UIViewAuto view in viewAutos) {
						autoSize (view, scaleX, y, heightOffset);
				}

				foreach (UIPanel view in panelAutos) {
						autoPanel (view, scaleX, y, heightOffset);
				}

				return true;
		}

		public static bool autoFix (GameObject view)
		{
				UIFix fix = view.GetComponent<UIFix> ();
				if (fix == null) {
						view.AddComponent <UIFix> ();
			
				} else if (fix.enabled) {
						return true;
			
				} else {
						fix.enabled = true;
				}
		
				return false;
		}

		public static void setAutoView (Transform trans, float scaleX, float y)
		{
				setAutoView (trans, scaleX, y, 0);
		}
	
		public static void setAutoView (Transform trans, float scaleX, float y, float autoHeight)
		{
				UIPanel panel = trans.gameObject.GetComponent<UIPanel> ();
				if (panel == null || !panel.enabled || panel.clipping == UIDrawCall.Clipping.None) {
						UIViewAuto view = trans.gameObject.GetComponent<UIViewAuto> ();
						if (autoHeight > 0 && view == null) {
								view = trans.gameObject.AddComponent<UIViewAuto> ();
								view.contentSize.y = autoHeight;
								// view.setHeight(y);
						}
			
						if (view != null && view.enabled) {
								autoSize (view, scaleX, y, y / scaleX - y);

						} else {
								if (trans.localPosition.y != 0) {
										autoPos (trans, scaleX, y);
								}
						}

				} else {
						autoPanel (panel, scaleX, y, y / scaleX - y);
				}
		}
	
		public static void setAutoViews (Transform trans, float scaleX, float y)
		{
				foreach (Transform view in trans) {
						if (view.localPosition.y != 0) {
								autoPos (view, scaleX, y);
						}
				}
		}

		protected static void autoPos (Transform view, float scaleX, float y)
		{
				Vector3 localPosition = view.localPosition;
				y *= 0.5f - 0.5f / scaleX;
				UIPos pos = view.gameObject.GetComponent<UIPos> ();
				if (pos == null) {
						if (view.localPosition.y > 0) {
								localPosition.y -= y;
				
						} else {
								localPosition.y += y;
						}
			
				} else {
						if (pos.posType == UIPos.PosType.Top) {
								localPosition.y -= y;
						} else {
								localPosition.y += y;
						}
				}

				view.localPosition = localPosition;
		}

		protected static void autoSize (UIViewAuto view, float scaleX, float y, float heightOffset)
		{
				view.setAutoHeight (view.contentSize.y + heightOffset);
		}

		protected static void autoPanel (UIPanel view, float scaleX, float y, float heightOffset)
		{
				Vector4 clipRange = view.clipRange;
				clipRange.w += heightOffset;
				if (clipRange.w < 1) {
						clipRange.w = 1;
				}

				view.clipRange = clipRange;
		}

		public static void addView (Transform viewTrans, Transform containerTrans)
		{
				Vector3 localScale = viewTrans.localScale;
				Vector3 localPosition = viewTrans.localPosition;
				viewTrans.parent = containerTrans;
				viewTrans.localScale = localScale;
				viewTrans.localPosition = localPosition;
		}

		public static void removeView (Transform viewTrans)
		{
				UIRetain retain = viewTrans.gameObject.GetComponent<UIRetain> ();
				if (retain == null || retain.getRetainCount () <= 0) {
						GameObject.Destroy (viewTrans.gameObject);

				} else {
						addView (viewTrans, UIScreen.getUnactiveGameObject ().transform);
				}
		}

		public static void addViewAuto (Transform viewTrans, Transform containerTrans)
		{
				addViewAuto (viewTrans, containerTrans, false);
		}
	
		public static void addViewAuto (Transform viewTrans, Transform containerTrans, bool autoHeight)
		{
				addView (viewTrans, containerTrans);
				UIViewAuto viewAuto = containerTrans.gameObject.GetComponent<UIViewAuto> ();
				if (viewAuto != null) {
						viewAuto.setAutoView (viewTrans, autoHeight ? viewAuto.contentSize.y : 0);
				}
		}

		public static void doAppear (UIViewor viewor, Transform containerTrans)
		{
				//viewor.transform.localPosition = Vector3.zero;
				addViewAuto (viewor.gameObject.transform, containerTrans, true);
				viewor.doAppear ();
		}

		public static void doDisappear (UIViewor viewor)
		{
				removeView (viewor.gameObject.transform);
				viewor.doDisappear ();
		}
	
		public static void doAppearGameObject (GameObject gameObject, Transform containerTrans)
		{
				doAppear (GameObjectUtils.getGameObjectComponent<UIViewor> (gameObject), containerTrans);
		}

		public static UIViewor getViewor (GameObject gameObject)
		{
				return ComponentUtils.fetchCurrentComponent<UIViewor> (gameObject);
		}

		public static UImNavViewor getNavViewor (GameObject gameObject)
		{
				return ComponentUtils.fetchCurrentComponent<UImNavViewor> (gameObject);
		}

		public static UImTabViewor getTabViewor (GameObject gameObject)
		{
				return ComponentUtils.fetchCurrentComponent<UImTabViewor> (gameObject);
		}

		public static void openDialog (GameObject gameObject)
		{
				openDialog (gameObject, false);
		}

		public static void openDialog (GameObject gameObject, bool customer)
		{
				Stack<GameObject> dialogObjectStack = UIScreen.getDialogObjectStack ();
				GameObject dialogBackGroundObject = UIScreen.getDialogBackGroundObject (customer);
				Vector3 localePosition = gameObject.transform.localPosition;
				if (dialogObjectStack.Count == 0) {
						float minZ = 0;
						foreach (Transform trans in UIScreen.getScreenObject().transform) {
								float z = trans.localPosition.z;
								if (minZ > z) {
										minZ = z;
								}
						}

						localePosition.z = minZ - UISort.BEHIND_STEP_D;
						UIScreen.showDialogBackGroundObject (dialogBackGroundObject);

				} else {
						GameObject dialogObject = dialogObjectStack.Peek ();
						localePosition.z = dialogObject.transform.localPosition.z - UISort.BEHIND_STEP_D;
				}

				Vector3 dialogPosition = dialogBackGroundObject.transform.localPosition;
				dialogPosition.z = localePosition.z;
				dialogBackGroundObject.transform.localPosition = dialogPosition;
		
				localePosition.z -= UISort.BEHIND_STEP_D;
				gameObject.transform.localPosition = localePosition;
		
				UIScreen.getDialogObjectStack ().Push (gameObject);
				addViewAuto (gameObject.transform, UIScreen.getScreenObject ().transform);
		}

		public static void closeDialog ()
		{
				Stack<GameObject> dialogObjectStack = UIScreen.getDialogObjectStack ();
				if (dialogObjectStack.Count > 0) {
						GameObject dialogObject = dialogObjectStack.Pop ();
						if (dialogObject != null) {
								removeView (dialogObject.transform);
								if (dialogObjectStack.Count == 0) {
										UIScreen.closeDialogBackGroundObject ();
								}
						}
				}
		}

		public static void closeDialogAll ()
		{
				Stack<GameObject> dialogObjectStack = UIScreen.getDialogObjectStack ();
				int count = dialogObjectStack.Count;
				if (count == 0) {
						return;
				}

				GameObject dialog;
				while (count-- > 0) {
						dialog = dialogObjectStack.Pop ();
						removeView (dialog.transform);
				}

				UIScreen.closeDialogBackGroundObject ();
		}

		public static GameObject currectDialog ()
		{
				return UIScreen.getDialogObjectStack ().Peek ();
		}
}
